package controller;

import com.mysql.cj.Session;
import pojo.Emp;
import service.IEmpService;
import service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class EmpController extends HttpServlet {

    private IEmpService ies=new EmpServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //super.service(req, resp);
        String requestURI = request.getRequestURI();
        int index = requestURI.lastIndexOf("/") + 1;
        String substring = requestURI.substring(index);
        Class<EmpController> empControllerClass = EmpController.class;
        Method method = null;

        try {
            method=empControllerClass.getMethod(substring,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }


    public void emplogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("text/html;charset=utf-8");

        String empno = request.getParameter("empno");
        String ename = request.getParameter("ename");

        Emp login = ies.Login(new Emp(Integer.parseInt(empno), ename));

        HttpSession session=request.getSession();//获得作用域对象

        if (login!=null){

            ArrayList<Emp> emps = ies.queryAll();

            session.setAttribute("emps",emps);
            session.setAttribute("mess","登录成功");
            response.sendRedirect("show.jsp");
        }else {
            session.setAttribute("mess","登录失败");
            response.sendRedirect("main.jsp");
        }
    }

    public void empdel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("text/html;charset=utf-8");

        String empno = request.getParameter("empno");

        boolean b = ies.removeByEmpno(Integer.parseInt(empno));

        HttpSession session=request.getSession();//获得作用域对象
        if (b){
            //response.getWriter().println("<script type='text/javascript'>alert('删除成功 ok');</script>");
            session.setAttribute("mess","删除成功");
        }else {
            session.setAttribute("mess","删除失败");
           // response.getWriter().println("<script type='text/javascript'>alert('删除失败 not ok');</script>");
        }

        ArrayList<Emp> emps = ies.queryAll();

        session.setAttribute("emps",emps);
        response.sendRedirect("show.jsp");
    }
}
